using MediatR;
using Microsoft.EntityFrameworkCore;
using Process.Api.Application.Command.BOM;
using Process.DAL;
using Process.ErrorCod;
using Process.Infastructure;
using Process.Model;

namespace Process.Api.Application.Handler.BOM
{
    /// <summary>
    /// 获取BOM详情处理器
    /// </summary>
    public class GetBomDetailHandler : IRequestHandler<GetBomDetailCommand, ApiResult<BomDetailDto>>
    {
        private readonly ProcessDbContext _context;

        public GetBomDetailHandler(ProcessDbContext context)
        {
            _context = context;
        }

        public async Task<ApiResult<BomDetailDto>> Handle(GetBomDetailCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 获取BOM基本信息
                var bomInfo = await _context.bomTables
                    .FirstOrDefaultAsync(x => x.Id == request.Id && !x.IsDelete, cancellationToken);

                if (bomInfo == null)
                {
                    return new ApiResult<BomDetailDto>
                    {
                        Code = ErrorStatus.Error,
                        Message = "BOM不存在",
                        Data = null
                    };
                }

                var result = new BomDetailDto
                {
                    BomInfo = bomInfo
                };

                // 获取BOM配件列表
                var goodsRelations = await _context.bomGoodsRelations
                    .Where(x => x.BomTableId == request.Id && !x.IsDelete)
                    .ToListAsync(cancellationToken);

                foreach (var relation in goodsRelations)
                {
                    var goodsInfo = await _context.goodsinfos
                        .FirstOrDefaultAsync(x => x.Id == relation.GoodsinfoId && !x.IsDelete, cancellationToken);

                    if (goodsInfo != null)
                    {
                        result.GoodsList.Add(new BomGoodsDetailDto
                        {
                            GoodsCode = goodsInfo.GoodsCode,
                            GoodsName = goodsInfo.GoodsName,
                            GoodsModel = goodsInfo.GoodsModel,
                            Unit = goodsInfo.Unit,
                            GoodsType = goodsInfo.GoodsType,
                            MinStock = goodsInfo.MinStock,
                            MaxStock = goodsInfo.MaxStock,
                            Stock = goodsInfo.Stock,
                            Price = goodsInfo.Price,
                            Status = goodsInfo.Status,
                            Remark = goodsInfo.Remark
                        });
                    }
                }

                // 获取BOM工艺流程列表
                var flowRelations = await _context.bomFlowRelations
                    .Where(x => x.BomTableId == request.Id && !x.IsDelete)
                    .ToListAsync(cancellationToken);

                foreach (var relation in flowRelations)
                {
                    var processFlowInfo = await _context.processes
                        .FirstOrDefaultAsync(x => x.Id == relation.ProcessFlowId && !x.IsDelete, cancellationToken);

                    if (processFlowInfo != null)
                    {
                        result.FlowList.Add(new BomFlowDetailDto
                        {
                            FlowCode = processFlowInfo.FlowCode,
                            FlowName = processFlowInfo.FlowName,
                            FlowNumber = processFlowInfo.FlowNumber,
                            Responsible = processFlowInfo.Responsible,
                            Department = processFlowInfo.Department,
                            Status = processFlowInfo.Status,
                            Remark = processFlowInfo.Remark
                        });
                    }
                }

                return new ApiResult<BomDetailDto>
                {
                    Code = ErrorStatus.Success,
                    Message = "获取BOM详情成功",
                    Data = result
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<BomDetailDto>
                {
                    Code = ErrorStatus.Error,
                    Message = $"获取BOM详情失败: {ex.Message}",
                    Data = null
                };
            }
        }
    }
}
